//+------------------------------------------------------------------+
//|                                                        trend.mq4 |
//|                                         Dimitri Rudel 28.10.2013 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimitri Rudel 17.11.2013"
#property link      ""

#include <basic3.mqh>

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern int exCntMinBar = 10;
extern int exCntBreak = 0;
extern double exTakeProfit = 100;
extern double exRetracement = 0.4;
extern double exAdx = 14;
extern double exMinAdx = 25;
extern double exMaxSlp = 0.004;//bei 1.32 50 pips

extern int exShortTf = 5;
extern int exLongTf = 30;

int timeFrame[2];



//trend varsf


datetime oldTime[2];
int trend[2];



int cntTrend[2];
int cntBreak[2];
int cntLow[2];
int cntHigh[2];

double high[2];
double low[2];
double tmpLow[2];
double tmpHigh[2]; 


datetime dtHigh[2];
datetime dtLow[2];

double move = 0;  
double retracement[2];



int cntOpenedTrade = 0;

bool broken = false;



string lastPoint[];

//other vars
double adx;
int magicNumber;

//draw vars
double gap = 160;
int row = 1;

bool initTrend = false;
int init()
  {
      magicNumber = 1000;
      magicNumber += Period();
      
      Print("trend");
      Print("timeFrame: ",Period());
      Print("magicNumber: ",magicNumber);
      


      

      fake();
    
   return(0);
  }
  
void initTrend(int tfi, int tf){
      
      timeFrame[tfi] = tf;

      oldTime[tfi] = iTime(0,tf,0);

      trend[tfi] = 0;  
      
      
      cntTrend[tfi] = 1;
      cntBreak[tfi] = 0;
      cntLow[tfi] = 1;
      cntHigh[tfi] = 1;
      
      dtHigh[tfi] = iTime(0,tf,0);
      dtLow[tfi] = iTime(0,tf,0);
      low[tfi] = Low[iLowest(0,exShortTf,0,MODE_LOW,exCntMinBar)];
      high[tfi] = High[iHighest(0,exShortTf,0,MODE_HIGH,exCntMinBar)];
      tmpLow[tfi] = low[tfi];
      tmpHigh[tfi] = high[tfi];    
      
      lastPoint[tfi] = "null";
      

  }

int start(){
   if(iBars(Symbol(),30)>1){
           int tf = 5; 
     Print("tfr: ",tf);
     Print("tf: ",iLow(0,tf,0));
     
     tf = 30; 
     Print("tfr: ",tf);
     Print("tf: ",iLow("EURUSD",30,0));
   }

   if(Bars > exCntMinBar && !initTrend){
      initTrend(0, exShortTf);
      initTrend(1, exLongTf);
      initTrend = true;
   }if(!initTrend){
      return (0);
   }return(0);
   row = 1;
   row = draw_new_row("trend0",trend[0],row);
   row = draw_new_row("high",DoubleToStr(high[0],Digits),row);
   row = draw_new_row("low",DoubleToStr(low[0],Digits),row);
   
   row++;
   
   row = draw_new_row("trend1",trend[1],row);
   row = draw_new_row("high",DoubleToStr(high[1],Digits),row);
   row = draw_new_row("low",DoubleToStr(low[1],Digits),row);
   row = draw_new_row("tmpLow",DoubleToStr(tmpLow[1],Digits),row);
 
   bool newTrend = startTrend(0);
   startTrend(1);

     for(int i=0; i < OrdersTotal(); i++) {
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
              if ( OrderSymbol()==Symbol() && OrderMagicNumber() == magicNumber) {
                   if(OrderType() == OP_BUY){
                     if(low[0] !=  OrderStopLoss()){
                        b_OrderModify(OrderTicket(),OrderOpenPrice(),low[0],OrderTakeProfit(),0,Blue);
                     }
                   }else if(OrderType() == OP_SELL){
                     if(high[0] !=  OrderStopLoss()){
                        b_OrderModify(OrderTicket(),OrderOpenPrice(),high[0],OrderTakeProfit(),0,Blue);
                     }
                   }             
              }
         }
     }
     bool newTrade = newTrend && b_isTradeTime() && trend[0] == trend[1]; 
     
     if(newTrade){
         b_OrderDeleteByMagic(magicNumber);
         double price = 0;
         double sl = 0;
         int cmd = -1;
         double tp;       
         if(trend[0] == 0){
            price = high[0] + Ask - Bid;
            sl = low[0] - (Point * (Ask-Bid));
            cmd = OP_BUYSTOP;
            if(price - sl > exMaxSlp * price){
               sl = price - (price * exMaxSlp);
            }  
         }else if(trend[0] == 1){
            price = low[0];
            cmd = OP_SELLSTOP;
            sl = high[0] + Point * (Ask-Bid);
            
            if(sl - price > exMaxSlp * price){

               sl = price + (price * exMaxSlp);
            }
         }else{
            return(1);
         }
         
         b_OrderSend(Symbol(),cmd,0.1,price,3,sl,0,"trend",magicNumber,0); 
     }

}
  

bool startTrend(int tfi)
  {   
     int tf = timeFrame[tfi];
      

        
      
     if(oldTime[tfi] == iTime(0,tf,0)){
         return(0);   
     }
     oldTime[tfi] = iTime(0,tf,0);
     
     bool newTrend = false;
     if(trend[tfi] == 0 && iLow(0,tf,1) < low[tfi])
     {  
       trend[tfi] = 1; 
       drawV(iTime(0,tf,1),"DOWN"+cntTrend[tfi]+" tf:"+tf);
       cntTrend[tfi]++;
       cntBreak[tfi] = 0;
       newTrend = true;
     }else if(trend[tfi] == 1 && iHigh(0,tf,1) > high[tfi])
     {
    
       trend[tfi] = 0; 
       drawV(iTime(0,tf,1),"UP"+cntTrend[tfi]);
       cntTrend[tfi]++; 
       cntBreak[tfi] = 0;
       newTrend = true;
     }
     if(newTrend){
         retracement[tfi] = 0;
     }



     bool newHigh = false;
     bool newLow = false;
 
     if(iLow(0,tf,1) < tmpLow[tfi]){
         tmpLow[tfi] = iLow(0,tf,1);
         dtLow[tfi] = iTime(0,tf,1);
         cntLow[tfi]++;
         newLow = true;             
     }    

     if(iHigh(0,tf,1) > tmpHigh[tfi]){
         tmpHigh[tfi] = iHigh(0,tf,1);
         dtHigh[tfi] = iTime(0,tf,1);
         cntHigh[tfi]++;
         newHigh = true;

         //double tmpRetracement = (high - tmpLow)/move;        
     }
     
     
     if(newHigh){
         //drawA(tmpHigh + gap * Point,dtHigh,"high: "+cntHigh+" move: "+move, SYMBOL_ARROWUP, Orange); 
         if(trend[tfi] == 0){         
            if(tmpHigh[tfi] > high[tfi]  && lastPoint[tfi] != "low"){

               drawA(tmpLow[tfi] - gap * Point,dtLow[tfi],"low: "+cntLow[tfi]+" retracement: "+retracement[tfi]+" tf"+tf, SYMBOL_ARROWDOWN, Red);
               low[tfi] = tmpLow[tfi];
               lastPoint[tfi] = "low";
               cntBreak[tfi]++;
            } 

               tmpLow[tfi] = iHigh(0,tf,1);
               dtLow[tfi] = iTime(0,tf,1);

         }else{
            double move = high[tfi] - tmpLow[tfi];
            retracement[tfi] = 0;
            if(move > 0){
               retracement[tfi] = (tmpHigh[tfi] - tmpLow[tfi])/move;
            }
            if(retracement[tfi] >= exRetracement && lastPoint[tfi] != "low"){
               if(low[tfi] != tmpLow[tfi]){
                  
                  drawA(tmpLow[tfi] - gap * Point,dtLow[tfi],"low: "+cntLow[tfi]+" move: "+retracement[tfi] +" tf"+tf, SYMBOL_ARROWDOWN, Red);
                  //hier ist tmpLow 0;
                                 
                  low[tfi] = tmpLow[tfi];
                  
                  lastPoint[0] = "low"; 
               }
            }

         }
     }else if(newLow){
         if(trend[tfi] == 0){
            move = tmpHigh[tfi] - low[tfi];
            retracement[tfi] = 0;
            if(move > 0){
               retracement[tfi] = (tmpHigh[tfi] - tmpLow[tfi])/(move);
            }
            if(retracement[tfi] >= exRetracement && lastPoint[tfi] != "high"){           
               if(high[tfi] != tmpHigh[tfi]){
                  
                  drawA(tmpHigh[tfi] + gap * Point,dtHigh[tfi],"high: "+cntHigh[tfi]+" move: "+retracement[tfi]+" tf"+tf, SYMBOL_ARROWUP, Green);
                  high[tfi] = tmpHigh[tfi]; 
                  lastPoint[tfi] = "high";
               }       
            }    
         }else{
            if(tmpLow[tfi] < low[tfi] && lastPoint[tfi] != "high"){

                  drawA(tmpHigh[tfi] + gap * Point,dtHigh[tfi],"high: "+cntLow[tfi]+" Retracement: "+retracement[tfi] +" tf"+tf, SYMBOL_ARROWUP, Green);
                  high[tfi] = tmpHigh[tfi];
                  lastPoint[tfi] = "high";
                  cntBreak[tfi]++;
            } 
              tmpHigh[tfi] = iHigh(0,tf,1);
              dtHigh[tfi] = iTime(0,tf,1);
         }
         //drawA(tmpLow - gap * Point,dtLow,"low: "+cntLow+" Retracement: "+retracement, SYMBOL_ARROWDOWN, Orange); 
     }
     return (newTrend);

}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  
void drawA(double price,datetime dt,string name = "arrow", int arrowCode = SYMBOL_ARROWUP, color col = Black){  
        if(!exDebug){
            return;
        }
   ObjectCreate(name,OBJ_ARROW,0,dt,price);  
   ObjectSet(name,OBJPROP_COLOR,col);
   ObjectSet(name,OBJPROP_STYLE,STYLE_SOLID); 
   ObjectSet(name, OBJPROP_ARROWCODE, arrowCode);
}

void drawV(datetime dt,string name = "line",color col = Black){ 
        if(!exDebug){
            return;
        } 
   
      ObjectCreate(name,OBJ_VLINE,0,dt,0);
      ObjectSet(name,OBJPROP_STYLE,STYLE_DOT);  
      ObjectSet(name,OBJPROP_COLOR,col);
}
  
void drawH(double price,string name = "line",int type = OBJ_HLINE, color col = Black){
        if(!exDebug){
            return;
        }     
      ObjectDelete(name);
      ObjectCreate(name,OBJ_HLINE,0,0,price);
      ObjectSet(name,OBJPROP_STYLE,STYLE_DASH);  
      ObjectSet(name,OBJPROP_COLOR,col);
}



int draw_new_row(string col1, string col2,int row)
{
        if(!exDebug){
            return (0);
        }
        string objName = "a"+row;
        color c= Black;
        int corner_position = 1;
        int fontsize = 16;
        int col1_xdist = 50;
        int y_offset = 5;
        int col2_xdist = 2;
        string fontname = "Times New Roman";


        ObjectCreate(objName,OBJ_LABEL,0,0,0);
        ObjectSet(objName,OBJPROP_CORNER,corner_position);
        ObjectSet(objName,OBJPROP_XDISTANCE,col1_xdist+(fontsize*2.7));
        ObjectSet(objName,OBJPROP_YDISTANCE,row*(fontsize+2)+y_offset+50);
        ObjectSetText(objName,col1,fontsize,fontname,c);
        

         objName = "b"+row;

        c=Red;

        ObjectCreate(objName,OBJ_LABEL,0,0,0);
        ObjectSet(objName,OBJPROP_CORNER,corner_position);
        ObjectSet(objName,OBJPROP_XDISTANCE,col2_xdist);
        ObjectSet(objName,OBJPROP_YDISTANCE,row*(fontsize+2)+y_offset+50);
        ObjectSetText(objName,col2,fontsize,fontname,c);
        
        return(row+1);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+